<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AddBatchOrder</title>
    <script src="../Public/DateFormat.js" type="text/javascript"></script>

</head>
<body>
<form class="form-horizontal" role="form" style="padding:15px;">
    <div class="form-group" >
        <label class="col-xs-4 control-label">批指令号：</label>
        <div class="col-xs-8">
            <input type="text" class="form-control" id="BatchOrder_Code" placeholder="请输入批指令号">
        </div>
    </div>

    <div class="form-group" >
        <label class="col-xs-4 control-label">批号：</label>
        <div class="col-xs-8">
            <input type="text" class="form-control" id="Batch_Code" placeholder="请输入批号">
        </div>
    </div>
    <div class="form-group" >
        <label class="col-xs-4 control-label">批指令类型：</label>
        <div class="col-xs-8">
            <select class="form-control" id="BatchOrder_Type">
                <option value="1">正式</option>
                <option value="2">模拟</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label for="Product_Code" class="col-xs-4 control-label">产品代码：</label>
        <div class="col-xs-8">
            <div class="input-group">
                <input type="text" class="form-control" id="Product_Code" placeholder="请选择" readonly="true">
                <span class="input-group-btn"><button id="Product_Select" class="btn btn-default" type="button">选择</button></span>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label for="Product_Name" class="col-xs-4 control-label">产品名称：</label>
        <div class="col-xs-8">
            <input type="text" class="form-control" id="Product_Name" readonly="true" placeholder="请选择">
        </div>
    </div>

    <div class="form-group">
        <label class="col-xs-4 control-label" for="Product_Spec">产品规格：</label>
        <div class="col-xs-8">
            <input type="text" class="form-control" id="Product_Spec" readonly="true" placeholder="请选择">
        </div>
    </div>
    <div class="form-group" >
        <label for="Recipe_Select" class="col-xs-4 control-label">配方：</label>
        <div class="col-xs-8">
            <div class="input-group">
                <input type="text" class="form-control" id="Recipe_Select" placeholder="请选择配方" readonly="true">
                <span class="input-group-btn"><button id="Recipe_Select_Btn" class="btn btn-default" type="button">选择</button></span>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label for="Product_Unit" class="col-xs-4 control-label">批量：</label>
        <div class="col-xs-8">
            <div class="input-group">
                <input type="text" class="form-control unit" id="Batch_Quantity"  placeholder="请输入批量">
                <span id="Product_Unit" class="input-group-addon"></span>
            </div>
        </div>
    </div>
    <div class="form-group" >
        <label class="col-xs-4 control-label">计划生产日期：</label>
        <div class="col-xs-8">
            <input type="date" name="bday" class="form-control" id="BatchOrder_PlanDate">
        </div>
    </div>

    <div class="form-group">
        <label class="col-xs-4 control-label">备注：</label>
        <div class="col-xs-8">
            <textarea id="Order_Description" class="form-control" placeholder="请输入理由" rows="3"></textarea>
        </div>
    </div>

    <div class="form-group" style = "margin-top: 15px;margin-bottom: 0px">
        <div class="col-xs-6" style="text-align: right">
            <button type="button" id="Add_BatchOrder_Confirm" class="btn btn-custom close-submit">确定</button>
        </div>
        <div class="col-xs-6" style="text-align: left">
            <button type="button" id="Add_BatchOrder_Cancel" class="btn btn-custom close-layer">取消</button>
        </div>
    </div>
</form>

<script type="text/javascript">
    $(function(){
        var fom = new ibcpDateFormat();
        fom.getServerDate("yyyy-MM-dd",function(dateVal){
            $('#BatchOrder_PlanDate').attr('min', dateVal);
        });

        //产品代码选择按钮
        $('#Product_Select').on('click', function(){
            ibcpSelectProduct.CallProduct(GetSelectProduct);
        });

        //配方选择按钮
        $('#Recipe_Select_Btn').on('click', function(){
            if($('#Product_Code').val() == ''){
                ibcpLayer.ShowTips('请选择产品信息',$('#Product_Select'));
                return;
            }
            TuLayer.ShowDiv('SelectRecipe.html', '选择配方', '700px', '540px');
        });

        //确认提交按钮
        $('#Add_BatchOrder_Confirm').on('click', function(){
            var orderData = {
                bodCode: $('#BatchOrder_Code').val(),       //批指令号
                batchCode: $('#Batch_Code').val(),          //批号
                batchQuantity: $('#Batch_Quantity').val(),  //数量
                bodTypeId: $('#BatchOrder_Type').find("option:selected").val(),       //种类id
                bodType: $('#BatchOrder_Type').find("option:selected").text(),         //种类名称
                proId: $('#Product_Code').attr('ibcdata'),  //产品id
                proCode: $('#Product_Code').val(),          //产品代码
                proName: $('#Product_Name').val(),          //产品名称
                proSpec: $('#Product_Spec').val(),          //产品规格
                unitId: $('#Product_Unit').attr('uomId'), //单位id
                unitName: $('#Product_Unit').val(),         //单位名称
                recipeId: $('#Recipe_Select').attr('ibcdata'),      //配方id
                recipeName: $('#Recipe_Select').val(),              //配方名称
                planDate: $('#BatchOrder_PlanDate').val() + " " + "00:00:00.00",     //计划时间
                description: $('#Order_Description').val()
            };
            addBathOrderCmd(orderData);
        });
    });

    //添加批指令方法
    function addBathOrderCmd(data) {
        'use strict';
        var check = CheckDataEmptyTip();
        if(check == false) return;
        signPath = '../Public/';
        Signature.CallSign(100, function (sid) {
            var datas = {
                'orderNo': data.bodCode,
                'batchNo': data.batchCode,
                'planQuantity': data.batchQuantity,
                'typeId': data.bodTypeId,
                'materialCodeId': data.proId,
                'uomId': data.unitId,
                'recipeId': data.recipeId,
                'planDate': data.planDate,
                'description': data.description,
                'sid': sid
            };
            var url = serverPath + "Order/createOrder";
            ibcpAjax.Insert(url, datas, false, function(result) {
                TuLayer.ShowOK("保存成功！");
                Signature.CloseSign();
                TuLayer.Close();
                GetBatchOrderList();
            });
        });
    }

    //检查非空提示
    function CheckDataEmptyTip(){
        'use strict';
        var check = true;
        $('input textarea').each(function(){
            if($(this).val() == ""){
                ibcpLayer.ShowTips('请填写',$(this));
                check = false;
            }
        });
        return check;
    }

    //选择产品
    function GetSelectProduct(selectedData){
        var code = selectedData.code;
        var name = selectedData.name;
        var spec = selectedData.spec;
        var proId = selectedData.id;
        $('#Product_Code').val(code);
        $('#Product_Name').val(name);
        $('#Product_Spec').val(spec);
        $('#Product_Code').attr('ibcdata', proId);
    }

    //获取工艺配方
    function GetSelectedRecipe(selectedRecipe){
        var recipeId = selectedRecipe.id;
        var recipeName = selectedRecipe.name;
        var symbol = selectedRecipe.symbol;
        var uomId = selectedRecipe.uomId;
        $('#Recipe_Select').val(recipeName);
        $('#Recipe_Select').attr('ibcdata', recipeId);
        $('#Product_Unit').text(symbol);
        $('#Product_Unit').attr('uomId',uomId);
    }
</script>
</body>
</html>